package com.example.mybatisify.controller;

import com.example.mybatisify.entities.Document;
import com.example.mybatisify.healper.LuceneHelper;
import com.example.mybatisify.service.IDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author me
 * @since 2024-06-25
 */
@Controller
@RestController
@RequestMapping("/document")
public class DocumentController {

    @Autowired
    private IDocumentService documentService;
    // CRUD
    @PostMapping("/create")
    public ResponseEntity create(String rawText) {
        // create a new document
        Document document = new Document(rawText);
        // save the document
        if (documentService.save(document)){
            List<Document> documents = new ArrayList<>();
            try {
                LuceneHelper.constructIndex(documents, false);
            } catch (IOException e) {
                return ResponseEntity.badRequest().body("Document creation failed");
            }
            return ResponseEntity.ok("Document created successfully");
        } else {
            return ResponseEntity.badRequest().body("Document creation failed");
        }
    }

    @GetMapping("/getDocumentById")
    public ResponseEntity getDocumentById(@RequestParam int id) {
        // get the document by id
        Document document = documentService.getById(id);
        if (document != null){
            return ResponseEntity.ok(document);
        } else {
            return ResponseEntity.badRequest().body("Document not found");
        }
    }

    @GetMapping("/getAllDocuments")
    public ResponseEntity getAllDocuments() {
        // get all documents
        return ResponseEntity.ok(documentService.list());
    }

    @PutMapping("/refreshIndex")
    public ResponseEntity refreshIndex() {
        try {
            LuceneHelper.constructIndex(documentService.list(), true);
            return ResponseEntity.ok("Index refreshed successfully");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("Index refresh failed");
        }
    }

    @GetMapping("/search")
    // inverted indexing the documents
    public ResponseEntity searchDocuments(@RequestParam String field, @RequestParam String query){
        try {
            return ResponseEntity.ok(LuceneHelper.queryIndex(field, query));
        } catch (IOException e) {
            return ResponseEntity.badRequest().body("Search failed");
        }
    }

    @PutMapping("/update")
    public ResponseEntity updateDocument(@RequestParam int id, @RequestParam String rawText) {
        // get the document by id
        Document document = documentService.getById(id);
        if (document != null){
            // update the document
            document.setRawText(rawText);
            // save the document
            if (documentService.updateById(document)){
                return ResponseEntity.ok("Document updated successfully");
            } else {
                return ResponseEntity.badRequest().body("Document update failed");
            }
        } else {
            return ResponseEntity.badRequest().body("Document not found");
        }
    }

    @DeleteMapping("/delete")
    public ResponseEntity deleteDocument(@RequestParam int id) {
        // get the document by id
        Document document = documentService.getById(id);
        if (document != null){
            // delete the document
            if (documentService.removeById(id)){
                return ResponseEntity.ok("Document deleted successfully");
            } else {
                return ResponseEntity.badRequest().body("Document deletion failed");
            }
        } else {
            return ResponseEntity.badRequest().body("Document not found");
        }
    }

}
